package com.sgb.kjwl.view.ui.adapter.self.carMasters;

import android.content.Context;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.MyOfferEntity2;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.UserOperating;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: yangLei
 * @date: 2021/4/7 15:22
 * @Description: 普通货源-我的报价适配器
 */
public class NormalMyOfferAdapter extends BaseQuickAdapter<MyOfferEntity2, BaseViewHolder> {

    public NormalMyOfferAdapter(@Nullable List<MyOfferEntity2> data) {
        super(R.layout.item_normal_my_offer, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder viewHolder, MyOfferEntity2 entity) {
        //货物信息
        String goodsInfo = "";
        goodsInfo += CargoConstant.getLastGoodsTypeName(entity.getGoodsTypeName(), entity.getSecondGoodsTypeName());
        if (!TextUtils.isEmpty(entity.getWeight())) {
            goodsInfo += "/" + NumberUtil.trim0(entity.getWeight()) + "吨";
        }
        if (!TextUtils.isEmpty(entity.getVolume())) {
            goodsInfo += "/" + NumberUtil.trim0(entity.getVolume()) + "方";
        }
        //装货时间
        long startTime;
        long endTime;
//        StringBuilder dateStr = new StringBuilder();
        if (!TextUtils.isEmpty(entity.getShipmentTime())) {
//            dateStr.append(DateUtils.formatDate(entity.getShipmentTime(), "yyyy/MM/dd"));
            startTime = Long.parseLong(entity.getShipmentTime());
        } else {
            startTime = 0;
        }
        if (!TextUtils.isEmpty(entity.getShipmentEndTime())) {
//            dateStr.append("-").append(DateUtils.formatDate(entity.getShipmentEndTime(), "yyyy/MM/dd"));
            endTime = Long.parseLong(entity.getShipmentEndTime());
        } else {
            endTime = 0;
        }

        String date = CargoConstant.stampFormatListLoadTime(true, startTime
                , entity.getStartPointTime(), endTime, entity.getEndPointTime());

        //设置页面数据
        viewHolder.setText(R.id.offer_place, String.format("%s %s → %s %s",
                entity.getCityName(),
                entity.getDistrictName(),
                entity.getDestCityName(),
                entity.getDestDistrictName()))
                .setText(R.id.offer_goods, TextUtils.isEmpty(goodsInfo) ? "/" : goodsInfo)
                .setText(R.id.offer_goods_time, TextUtils.isEmpty(date) ? "/" : date)
                .setText(R.id.newest_freight_offer, "单价" + NumberUtil.trim0(entity.getFreight()) + entity.getFreightUnit())
                .setText(R.id.newest_freight_all, String.format("%s元", NumberUtil.trim0(entity.getAllFreight())))
                .setText(R.id.offer_price_time, !TextUtils.isEmpty(entity.getRecentQuotationTime())
                        ? DateUtils.formatDate(String.valueOf(entity.getRecentQuotationTime()), "yyyy/MM/dd HH:mm:ss")
                        : DateUtils.formatDate(String.valueOf(entity.getCreateTime()), "yyyy/MM/dd HH:mm:ss"))
//                .setText(R.id.is_normal, entity.getPattern() == 1 ? "竞价" : "普通")
//                .setBackgroundRes(R.id.is_normal, entity.getPattern() == 1 ? R.drawable.member_shape_gradient_1fd2ac2 : R.drawable.shape_blue_button)
                .addOnClickListener(R.id.fl_item)
                .addOnClickListener(R.id.tv_update_offer)
                .addOnClickListener(R.id.tv_pay_msg_fee)
                .addOnClickListener(R.id.tv_check_order)
                //隐藏失效原因
                .setVisible(R.id.ll_reason_for_failure, false);

        if (entity.getPattern() == 1) {
            viewHolder.setVisible(R.id.is_normal, true);
            viewHolder.setText(R.id.is_normal, "竞价");
            viewHolder.setBackgroundRes(R.id.is_normal, R.drawable.member_shape_gradient_1fd2ac2);
        } else {
            viewHolder.setVisible(R.id.is_normal, false);
        }

        //普通货源报价状态：1已报价 2待支付 3已支付 5已失效
        switch (entity.getOfferStatus()) {
            case 1://已报价
                viewHolder.setImageResource(R.id.offer_status, R.mipmap.icon_offer_tobe_confirmed)
                        .setVisible(R.id.offer_status, true)
                        .setVisible(R.id.bottom_bar, true)
                        .setVisible(R.id.tv_update_offer, true)
                        .setVisible(R.id.ll_pay_msg_fee, false)
                        .setVisible(R.id.ll_check_order, false);
                break;
            case 2://待支付
                viewHolder.setImageResource(R.id.offer_status, R.mipmap.offer_to_pay)
                        .setVisible(R.id.offer_status, true)
                        .setVisible(R.id.bottom_bar, true)
                        .setVisible(R.id.tv_update_offer, false)
                        .setVisible(R.id.ll_check_order, false);

                RelativeLayout time_layout = viewHolder.getView(R.id.time_layout);
                TextView tvTime = viewHolder.getView(R.id.time);
                RelativeLayout ll_pay_msg_fee = viewHolder.getView(R.id.ll_pay_msg_fee);
                TextView tv_pay_msg_fee_tip = viewHolder.getView(R.id.tv_pay_msg_fee_tip);

                if (UserOperating.getInstance().isCompany() && !TextUtils.isEmpty(entity.getOrderNo())) {
                    time_layout.setVisibility(View.VISIBLE);
                    ll_pay_msg_fee.setVisibility(View.GONE);
                    CountDownTimer cdt = new CountDownTimer(entity.getOrderPayTime(), 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            String str = String.format("%s%s", "剩余", DateUtils.formatDate(millisUntilFinished, "mm分ss秒"));
                            tvTime.setText(NumberUtil.trim0(entity.getMessagePrice()) + "元信息费支付中，请联系财务至资金中心-待付订单完成支付。剩余支付时间 " +
                                    ("0".equals(entity.getOrderPayTime()) ? "已超时" : str));
                        }

                        @Override
                        public void onFinish() {
                            tvTime.setText("已超时");
                        }
                    };
                    cdt.start();
                    allTimer.add(cdt);
                } else {
                    time_layout.setVisibility(View.GONE);
                    ll_pay_msg_fee.setVisibility(View.VISIBLE);
                    tv_pay_msg_fee_tip.setText(String.format("%s元", NumberUtil.trim0(entity.getMessagePrice())));
                }


                break;
            case 3://已支付
                viewHolder.setImageResource(R.id.offer_status, R.mipmap.offer_payed)
                        .setVisible(R.id.offer_status, true)
                        .setVisible(R.id.bottom_bar, true)
                        .setVisible(R.id.tv_update_offer, false)
                        .setVisible(R.id.ll_pay_msg_fee, false)
                        .setText(R.id.tv_check_order_msg_fee, String.format("%s元", NumberUtil.trim0(entity.getMessagePrice())))
                        .setVisible(R.id.ll_check_order, true);
                break;
            case 5://已失效
                viewHolder.setImageResource(R.id.offer_status, R.mipmap.offer_off_time)
                        .setVisible(R.id.offer_status, true)
                        .setVisible(R.id.bottom_bar, false)
                        .setVisible(R.id.ll_reason_for_failure, true)
                        .setText(R.id.tv_reason_for_failure, getFailStr(entity.getFailureCause()));
                break;
            default:
                viewHolder.setVisible(R.id.offer_status, false)
                        .setVisible(R.id.bottom_bar, false);
                break;
        }
    }

    ArrayList<CountDownTimer> allTimer = new ArrayList<>();

    public void setCancelTimer() {
        if (allTimer != null && allTimer.size() > 0) {
            for (CountDownTimer countDownTimer : allTimer) {
                countDownTimer.cancel();
            }
            allTimer.clear();
        }
    }

    /**
     * 获取失败原因
     */
    private String getFailStr(String type) {
        String str = "/";
        if (TextUtils.isEmpty(type)) {
            return str;
        }
        //失效原因  0 车主超时未支付信息费 / 1 货主未同意报价 / 2 货主已下架货源 / 3 货主已删除货源
        switch (type) {
            case "0":
                str = "车主超时未支付信息费";
                break;
            case "1":
                str = "货主未同意报价";
                break;
            case "2":
                str = "货主已下架货源";
                break;
            case "3":
                str = "货主已删除货源";
                break;
        }
        return str;
    }
}
